package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsGoodsType;
import com.jy.wms.service.WmsGoodsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: WmsGoodsTypeController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author JaneLiu
 * @Date Mon Sep 04 14:25:45 CST 2017
 */
@Controller
@RequestMapping("/wmsgoodstype")
@Api(value = "/wmsgoodstype", description = "关于货品类型的操作")
public class WmsGoodsTypeController extends BaseController<WmsGoodsTypeService,WmsGoodsType> {
    private static Logger log = LoggerFactory.getLogger(LoginController.class);
    @ApiOperation(value = "货品种类 POST请求查询货品种类",response = JSONObject.class, notes = "POST请求货品种类信息")
    @PostMapping(value = "/list")
    public @ResponseBody JSONObject getWmsGoodsType(@RequestBody WmsGoodsType entity,HttpSession session) {
        Page<WmsGoodsType> list=new Page<WmsGoodsType>();
        JSONObject json = getJSONObject();
       try{
           UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
           //获取当前仓库id
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));
           entity.setCkid(ckid);
           //获取当前操作用户的货主id
           List<SysHz> hz=(List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
           if (hz.size()>0){
               entity.setSshzid(hz.get(0).getHzid());
               list= baseService.baseSelectPage(entity);
           }
           json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                   ,(int) list.getTotal());
       }catch (Exception e){
           e.printStackTrace();
        }
            return json;
    }

    @ApiOperation(value = "货品种类添加",response = JSONObject.class, notes = "POST货品种类添加")
    @PostMapping(value = "goodsAdd")
    public @ResponseBody JSONObject addWmsGoodsType(@RequestBody WmsGoodsType entity, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            entity.setId(UUIDUtils.uuid());
            //获取当前操作用户的货主id
            //  String owner=getSessionInfo(SessionInfo.SessionConsignor).toString();
            // entity.setSshzid(owner);
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取当前仓库id
            // String ckid=userinfo.getUser().getRentId().toString();
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            entity.setCkid(ckid);
            //获取当前操作的用户货主id
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setSshzid(hz.get(0).getHzid());
            baseService.baseInsert(entity);
        }catch (SucessException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"货品种类添加成功","", JSONObject.fromObject(entity).toString());
         }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"货品种类添加失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"货品种类添加失败",e.getMessage(),"");
        }
        return json;
    }
    /**
     * 根据货主id查询货品种类
     * @param entityType
     * @return
     */

    @ApiOperation(value = "货品种类 查看单个货主的货品种类",response = JSONObject.class, notes = "GET查看单个货主的货品种类")
    @PostMapping(value = "/getWmsGoodsTypeByHzId")
    public @ResponseBody JSONArray getWmsGoodsTypeByHzId(@RequestBody WmsGoodsType entityType,HttpSession session) {
        JSONObject json = getJSONObject();
        List<WmsGoodsType> entity=  new ArrayList<WmsGoodsType>();

        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取当前仓库id
            entityType.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            //获取当前操作的用户货主id
            List<SysHz> hz=(List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            entityType.setSshzid(hz.get(0).getHzid());
            String mc="";
            if(entityType.getMc()!=null){
                mc=entityType.getMc();
            }
            entity = baseService.baseSelectBySshzid(entityType.getSshzid(),entityType.getMc(),entityType.getCkid());
            return JSONArray.fromObject(entity);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONArray.fromObject( SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.success.toString(),"货品种类","查看货主的货品种类失败",""));
        }

    }
}
